package com.zhq.blog.manage.domain.controller.user;

import javax.validation.Valid;

import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhq.blog.core.domain.user.User;
import com.zhq.blog.manage.domain.controller.BaseController;
import com.zhq.blog.manage.domain.controller.BossModel;

@Controller
@Scope("prototype")
@RequestMapping("/admin/user/user/*")
public class UserController extends BaseController {
	
	@RequestMapping()
    public BossModel index(String viewState,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE,sort = "id",direction = Sort.Direction.DESC) Pageable pageable){
		if(viewState == null || viewState.equals("")){
			return createModel("admin/user/user/list").add("usersPage",context.getUserRepository().findAll(User.idLike(viewState),pageable)).add("viewState", "");

		}else{
			return createModel("admin/user/user/list").add("usersPage",context.getUserRepository().findAll(User.idLike(viewState),pageable)).add("viewState", viewState);
		}
	}
	
	
	@RequestMapping(value="edit/{id}")
	public BossModel edit(@PathVariable long id){
		return createModel("admin/user/user/info").add("user",context.getUserRepository().findOne(id));
	}
	
	@RequestMapping(value="save",method = RequestMethod.POST)
	@InitBinder(value={"email","password","isVip","coin","coinBank","experience","headPic"})
	public BossModel save(@Valid final User user, final BindingResult bindingResult, final ModelMap model, RedirectAttributes redirectUsers,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE) Pageable pageable){
		if (bindingResult.hasErrors()) {
			return createModel("admin/user/user/info");
		}
		context.getUserRepository().save(user);
		String message = user.getSucessFlashMessage();
		redirectUsers.addFlashAttribute("message", message);
		return createModel("redirect:/admin/user/user/index");
		
	}
	
	@RequestMapping(value="delete",method= RequestMethod.POST)
	public BossModel delete(String viewState,long id,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE,sort = "id",direction = Sort.Direction.DESC) Pageable pageable){
		context.getUserRepository().delete(id);
		return createModel("admin/user/user/users_table").add("usersPage",context.getUserRepository().findAll(User.idLike(viewState),pageable)).add("viewState", viewState);
	}
	
	/**
	 * 跳转到填写冻结信息页面
	 */
	@RequestMapping(value = "add/{id}")
	public BossModel add(@PathVariable long id) {
		
		return createModel("admin/user/user/freeze").add("user", context.getUserRepository().findOne(id));
	}
	
	@RequestMapping(value = "freeze", method = RequestMethod.POST)
	public BossModel freeze(User user){
		User u = context.getUserRepository().findOne(user.getId());
		u.setFlag(false);
		u.setFreeze(user.getFreeze());
		return createModel("redirect:/admin/user/user/index");
	}
	


}
